package com.cxzl.pcxt.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cxzl.pcxt.dao.AnswerDetailDao;
import com.cxzl.pcxt.entity.AnswerDetail;

@Service("pcxtAnswerDetailService")
@Transactional
public class AnswerDetailService {

	@Resource(name = "pcxtAnswerDetail")
	@Autowired
	private AnswerDetailDao answerDetailDao;

	/**
	 * 试卷服务
	 */
	private PaperService paperService;

	private static Map<String, String> answerMap = new HashMap<String, String>();
	static {
		answer();
	}

	/**
	 * 
	 * @param details
	 *            答案明细
	 * @param userId
	 *            用户ID
	 * @param paperId
	 *            试卷ID
	 */
	public void answerPaper(List<AnswerDetail> details, int userId, int paperId) {
		paperService.findById(paperId);
		answerDetailDao.deleteByUserIdAndPaperId(userId, paperId);
		String answer = "";
		for (AnswerDetail detail : details) {
			detail.setUserId(userId);// 用户ID
			detail.setPaperId(paperId);// 试卷ID
			answer = getAnswer(detail.getQcode());
			detail.setAnswer(answer);// 标准答案
			detail.setValue(5);// 设置分值
			// 设置得分
			if (StringUtils.equalsIgnoreCase(answer, detail.getOpt())) {
				detail.setScore(5);
			} else {
				detail.setScore(0);
			}

			answerDetailDao.saveEntity(detail);
		}
	}

	/**
	 * 获取答案
	 * 
	 * @param code
	 * @return
	 */
	private String getAnswer(String code) {
		return answerMap.get(code);
	}

	public static void answer() {
		answerMap.put("cs1", "C");
		answerMap.put("cs2", "C");
		answerMap.put("cs3", "A");
		answerMap.put("cs4", "C");
		answerMap.put("cs5", "E");
		answerMap.put("cs6", "D");
		answerMap.put("cs7", "C");
		answerMap.put("cs8", "B");
		answerMap.put("cs9", "A");
		answerMap.put("cs10", "C");
		answerMap.put("cs11", "C");
		answerMap.put("cs12", "D");
		answerMap.put("cs13", "C");
		answerMap.put("cs14", "D");
		answerMap.put("cs15", "C");
		answerMap.put("cs16", "C");
	}

}
